exports.add = function(a,b){
    return a+b;
}
exports.mul = function(a,b){
    return a*b;
}
exports.sub = function(a,b){
    return a-b;
}

//  暴露方法
exports.hello = function (name){
    return 'hello ' + name;
};
exports.bye = function bye(name){
    return 'bye '+ name;
};

exports.getMime = function(extname){
    switch (extname){
        case '.html':
            return 'text/html';
            break;
        case '.css':
            return 'text/css';
            break;
        case '.js':
            return 'text/javascript';
            break;
        default:
            return 'text/html';
    }
}

// 获取后缀名
const fs = new require('fs');
let getFileMime = function(extname){
    /*  异步
    return new Promise(function(resolve,reject){
        fs.readFile('mime.json', function(err,data){
            if(!err){
                let mime = JSON.parse(data.toString())
                resolve(mime[extname]);
            } else{
                console.log(err);
            }
        })
    })
    */

    // 同步
   var data = fs.readFileSync('mime.json')
   let mime = JSON.parse(data.toString())
   return mime[extname];

}  //私有方法

const path = require('path')
const url = require('url')
exports.static = function(req,res,staticPath){
    //获取地址 test\html\电影卡片练习.html
    let pathname = url.parse(req.url).pathname;
    pathname = pathname == '/'?'index.html':pathname;   //设置默认为/index.html
    let extname = path.extname(pathname)  // path.extname()获取地址后缀
//通过fs模块读取文件
    if(pathname!='/favicon.ico'){
        // console.log(url.parse(request.url))
        // fs.readFile('test/' + pathname, async (err,data)=>{  异步
        /*
        fs.readFile(staticPath + pathname, (err,data)=>{    //同步
            if(err){
                console.log(err)
                res.writeHead(404,{'Content-Type': 'text/html;charset="utf-8"'})
                res.end('这个页面不存在')
            }
            // let mime = await tools.getFileMime(extname)   异步
            let mime = getFileMime(extname)   //同步
            // console.log(mime)
            res.writeHead(200, {'Content-Type': mime + ';charset="utf-8"'});
            res.end(data);
        });
        */

        // 路由
        
        try{
            var data = fs.readFileSync(staticPath + pathname)
            if(data){
                let mime = getFileMime(extname)   //同步
                // console.log(mime)
                res.writeHead(200, {'Content-Type': mime + ';charset="utf-8"'});
                res.end(data);
            }
        }catch (error){
            
        }
       
        
    }
}

// 路由封装

let app = {
    static:(req,res)=>{
        let pathname = url.parse(req.url).pathname;
        pathname = pathname == '/'?'index.html':pathname;   //设置默认为/index.html
        let extname = path.extname(pathname)  // path.extname()获取地址后缀
        //通过fs模块读取文件
        if(pathname!='/favicon.ico'){
            try{
                var data = fs.readFileSync(staticPath + pathname)
                if(data){
                    let mime = getFileMime(extname)   //同步
                    // console.log(mime)
                    res.writeHead(200, {'Content-Type': mime + ';charset="utf-8"'});
                    res.end(data);
                }
            }catch (error){
                
            }
        }
    },
    login: (req,res)=>{
        res.end('login');
    },
    news: (req,res)=>{
        res.end('news');
    },
    doLogin: (req,res)=>{
        res.end('doLogin');
    },
    error: (req,res)=>{
        res.end('error');
    }
}

module.exports = app

/*
    exports只能使用.的方式向外暴露内部变量
    exports.xxx = xxx;
    module.exports 既可. 亦可直接赋值
    module.exports.xxx = xxx
    module.exports = {}
*/

// module.exports = {
//     nam: "bob",
//     age: 20,
//     sayName: function(){
//         console.log("I'm bob");
//     }
// };